package com.bootdo.put.service.impl;

import com.alibaba.fastjson.JSON;
import com.bootdo.goods.dao.TGoodsAllocationDao;
import com.bootdo.goods.domain.TGoodsAllocationDO;
import com.bootdo.goods.service.TGoodsAllocationService;
import com.bootdo.provider.dao.GoodsInfoDao;
import com.bootdo.provider.domain.GoodsInfoDO;
import com.bootdo.provider.service.GoodsInfoService;
import com.bootdo.put.dao.PutStorageDao;
import com.bootdo.put.domain.PutStorageDO;
import com.bootdo.put.domain.dto.PutGoodsDto;
import com.bootdo.stock.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.put.dao.PutStorageDetailDao;
import com.bootdo.put.domain.PutStorageDetailDO;
import com.bootdo.put.service.PutStorageDetailService;
import org.springframework.transaction.annotation.Transactional;


@Service
@Transactional
public class PutStorageDetailServiceImpl implements PutStorageDetailService {
	@Autowired
	private PutStorageDetailDao putStorageDetailDao;

	@Autowired
	private TGoodsAllocationDao tGoodsAllocationDao;

	@Autowired
	private PutStorageDao putStorageDao;

	@Autowired
    private GoodsInfoDao goodsInfoDao;

	@Autowired
	private StockService stockService;



	
	@Override
	public PutStorageDetailDO get(Long putDetailId){
		return putStorageDetailDao.get(putDetailId);
	}
	
	@Override
	public List<PutStorageDetailDO> list(Map<String, Object> map){
		return putStorageDetailDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return putStorageDetailDao.count(map);
	}



	@Override
	public int save(PutStorageDetailDO putStorageDetail, Long providerId,String putType){

         int msg =stockService.update(putStorageDetail.getGoodsId(),putStorageDetail.getGoodsNumber(),0,0,0);

		System.out.println("msg:"+msg);


		if(!putType.equals("142001")){
			return putStorageDetailDao.save(putStorageDetail);
		}

		TGoodsAllocationDO tGoodsAllocationDO = tGoodsAllocationDao.getAllocation(providerId);
            putStorageDetail.setPutTime(new Date());
		//判断是否有货位
		if(tGoodsAllocationDO==null){
			putStorageDetail.setPutDetailStatus("121004");


		}else {
			tGoodsAllocationDO.setAllocationStatus("121002");
			tGoodsAllocationDO.setGoodsId(putStorageDetail.getGoodsId());
			tGoodsAllocationDO.setGoodsNumber(putStorageDetail.getGoodsNumber());
			tGoodsAllocationDao.update(tGoodsAllocationDO);
			putStorageDetail.setTargetGoodsArea(tGoodsAllocationDO.getAllocationId());
			putStorageDetail.setTargetGoodsArea(tGoodsAllocationDO.getAllocationId());
		}

		return putStorageDetailDao.save(putStorageDetail);
	}
	
	@Override
	public int update(PutStorageDetailDO putStorageDetail){
		return putStorageDetailDao.update(putStorageDetail);
	}
//	
//	@Override
//	public int remove(Long putDetailId){
//		return putStorageDetailDao.remove(putDetailId);
//	}
//	
//	@Override
//	public int batchRemove(Long[] putDetailIds){
//		return putStorageDetailDao.batchRemove(putDetailIds);
//	}

	@Override
	public PutStorageDetailDO detailByTrayCode(Map<String,Object> map){
		return putStorageDetailDao.detailByTrayCode(map);
	}


	@Override
	public void updatePut(String trayCode,Long forkliftId){
		//查询搬运状态的托盘
		Map<String,Object> map = new HashMap<>();
		map.put("trayCode",trayCode);
		//map.put("putDetailStatus","121002");
		PutStorageDetailDO putStorageDetailDO = putStorageDetailDao.detailByTrayCode(map);
		//修改仓位信息
		TGoodsAllocationDO tGoodsAllocationDO = tGoodsAllocationDao.getTargetAllocation(trayCode);
		//System.out.println(JSON.toJSON(putStorageDetailDO));
		tGoodsAllocationDO.setAllocationStatus("120003");
//        tGoodsAllocationDO.setGoodsId(putStorageDetailDO.getGoodsId());
//        tGoodsAllocationDO.setGoodsNumber(putStorageDetailDO.getGoodsNumber());
        tGoodsAllocationDao.update(tGoodsAllocationDO);

		System.out.println("putStorageDetailDO:"+JSON.toJSON(putStorageDetailDO));
        //修改入库单详情信息
		putStorageDetailDO.setPutDetailStatus("121003");
		putStorageDetailDO.setForkliftId(forkliftId);
		putStorageDetailDao.update(putStorageDetailDO);


        //判断入库单还有没有搬运完未入库的
		Map<String,Object> map1 = new HashMap<>();
		map1.put("putId",putStorageDetailDO.getPutId());
		map1.put("putDetailStatus","121002");

        List<PutStorageDetailDO> list =putStorageDetailDao.list(map1);
        //判断入库单还有没有未搬运的
		map1.put("putDetailStatus","121002");
		List<PutStorageDetailDO> list1 =putStorageDetailDao.list(map1);

		if(list.size()==0&&list1.size()==0){
			PutStorageDO putStorageDO = putStorageDao.get(putStorageDetailDO.getPutId());
			putStorageDO.setStatus("140003");
		}
	}

	@Override
	public List<PutGoodsDto> getGoodsByPut(Long putId) {
		return putStorageDetailDao.getGoodsByPut(putId);
	}

	@Override
	public List<PutGoodsDto> getPutDetail(Long putId) {
		List<PutGoodsDto> putGoodsDtoList=putStorageDetailDao.getGoodsByPut(putId);
		Map<String,Object> map ;
		for (PutGoodsDto p:putGoodsDtoList) {

			map=new HashMap<>();
			map.put("goodsId",p.getGoodsId());
			map.put("putId",putId);
            List<PutStorageDetailDO> list = putStorageDetailDao.list(map);
			p.setTrayNum(list.size());
            p.setPutStorageDetailDOList(list);
            Integer goodsNumber= 0;
            Integer scatteredNumber=0;
			for (PutStorageDetailDO putStorageDetailDO:list) {
				goodsNumber+=putStorageDetailDO.getGoodsNumber();
			}
			p.setScatteredNumber(scatteredNumber);
			p.setGoodsNumber(goodsNumber);

		}
		return putGoodsDtoList;
	}

	@Override
	public int countPsd(Long providerId) {
		return putStorageDetailDao.countPsd(providerId);
	}

	@Override
	public List<PutStorageDetailDO> getPsd(Long providerId) {
		return putStorageDetailDao.getPsd(providerId);
	}

	@Override
	public List<PutStorageDO> getPsdGroup() {
		return putStorageDetailDao.getPsdGroup();
	}

}
